/**
* @author zqp
* @version2017年12月23日 
* @description 
*/
package com.zqp.security.core.authentication.mobile;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

/**
* @author zqp
* @version2017年12月23日 
* @description 
*/
@Component
public class SmsCodeAuthenticationSecurityConfig  extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity>{

	
	@Autowired
	private AuthenticationSuccessHandler zqpAuthenticationSuccessHandler;
	@Autowired
	private AuthenticationFailureHandler zqpAuthenticationFailedHandler;
	
	
	
	@Autowired
	private UserDetailsService UserDetailsService;
	
	/* (non-Javadoc)
	 * @see org.springframework.security.config.annotation.SecurityConfigurerAdapter#configure(org.springframework.security.config.annotation.SecurityBuilder)
	 */
	@Override
	public void configure(HttpSecurity http) throws Exception {

		SmsCodeAuthenticationenFilter smsfilter = new SmsCodeAuthenticationenFilter();
		smsfilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
		smsfilter.setAuthenticationSuccessHandler(zqpAuthenticationSuccessHandler);
		smsfilter.setAuthenticationFailureHandler(zqpAuthenticationFailedHandler);
		
		SmsCodeAuthenticationProvider smsProvider = new SmsCodeAuthenticationProvider();
		smsProvider.setUserDetailsService(UserDetailsService);
		
		
		http.authenticationProvider(smsProvider)
			.addFilterAfter(smsfilter, UsernamePasswordAuthenticationFilter.class);
	}
}
